package com.stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Random;
import java.util.stream.Stream;

public class 创建_Stream {
	public static void main(String[] args) {
		
		//创建一个含有空元素的Stream
		Stream<String> stream1 = Stream.empty();
		//创建一个包含一个元素的Stream
		Stream<String> stream2 = Stream.of("test1");
		//创建一个包含多个元素的Stream
		Stream<String> stream3 = Stream.of("test1","test2","test3");
		stream1.forEach(System.out::println);
		stream2.forEach(System.out::println);
		stream3.forEach(System.out::println);
		
		
		/**
		 * 使用iterate(final T seed,final UnaryOperator<T> u)方法可以获取一个无限数量元素的Stream
		 * 所以需要使用limit、filter等来限制元素数量
		 */
		Stream<Integer> stream4 = Stream.iterate(1, t -> t +2).limit(10);
		stream4.forEach(System.out::println);
		
		/**
		 * 使用generate(Supplier<? extends T> s)方法可以获取一个无限且无序元素的Stream
		 */
		Stream<Integer> stream5 = Stream.generate(new Random()::nextInt).limit(10);
		stream5.forEach(System.out::println);
		
		
		/**
		 * 构建器builder()
		 * accept()添加一个元素，但是不能连续添加
		 * add()添加一个元素，可以连续添加
		 */
		Stream.Builder<String> builder = Stream.builder();
		builder.accept("test1");
		builder.add("test2").add("test3");
		Stream<String> stream6 = builder.build();
		stream6.forEach(System.out::println);
		
		/**
		 * java.util.Collection接口默认定义了stream()和parallelStream()方法
		 */
		Collection<String> c1 = new ArrayList<String>(Arrays.asList("test1","test2","test3")) ;
		Stream<String> stream7 = c1.stream();
		stream7.forEach(System.out::println);
		Stream<String> stream8 = c1.parallelStream();
		stream8.forEach(System.out::println);
		
		
		/**
		 * Arrays数组中定义了一系列获取Stream的方法
		 */
		Stream<String> stream9 = Arrays.stream(new String[] {"test1","test2"});
		stream9.forEach(System.out::println);
	}
}
